//
//  BDMessageListTableViewCell.m
//  iOS_beboy
//
//  Created by wen on 2018/1/30.
//  Copyright © 2018年 com.8xiong. All rights reserved.
//

#import "BDMessageListTableViewCell.h"

@implementation BDMessageListTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.nickName = [[UILabel alloc] init];
        [self.contentView addSubview:_nickName];
        
        _headView = [[UIImageView alloc] init];
        [self.contentView addSubview:_headView];
        
        _message = [[UILabel alloc] init];
        [self.contentView addSubview:_message];
       
        _time = [[UILabel alloc] init];
        [self.contentView addSubview:_time];
        
        _messageNumberLabel = [[UILabel alloc] init];
        [self.contentView addSubview:_messageNumberLabel];
        
        self.time.textColor = [UIColor grayColor];
        self.nickName.textColor = UIColorHex(0x3f80dd);
        self.message.textColor = UIColorHex(0x808080);
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    if (selected){
        self.messageNumberLabel.backgroundColor = UIColorHex(0xfa3e32);
    }
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    
    [super setHighlighted:highlighted animated:animated];
    
    if (highlighted){
        self.messageNumberLabel.backgroundColor = UIColorHex(0xfa3e32);
    }
}

- (void)setCellDataWithConversation:(JMSGConversation *)conversation {
    if (conversation.conversationType == kJMSGConversationTypeSingle) {
        [self.headView sd_setImageWithURL:[NSURL URLWithString:((JMSGUser *)conversation.target).avatar] placeholderImage:[UIImage imageNamed:@"avatar"]];
    }else if (conversation.conversationType == kJMSGConversationTypeGroup) {
        [self.headView sd_setImageWithURL:[NSURL URLWithString:((JMSGGroup *)conversation.target).avatar] placeholderImage:[UIImage imageNamed:@"avatar"]];
    }
    
    self.nickName.text = conversation.title;
    self.conversationId = [self conversationIdWithConversation:conversation];
    
    [conversation avatarData:^(NSData *data, NSString *objectId, NSError *error) {
        if (![objectId isEqualToString:_conversationId]) {
            NSLog(@"out-of-order avatar");
            return ;
        }
        
        if (error == nil) {
            if (data != nil) {
                [self.headView setImage:[UIImage imageWithData:data]];
            } else {
                if (conversation.conversationType == kJMSGConversationTypeSingle) {
                    [self.headView setImage:[UIImage imageNamed:@"headDefalt"]];
                } else {
                    [self.headView setImage:[UIImage imageNamed:@"talking_icon_group"]];
                }
            }
        } else {
            BDLog(@"fail get avatar");
        }
    }];
    
    if ([conversation.unreadCount integerValue] > 0) {
        [self.messageNumberLabel setHidden:NO];
        self.messageNumberLabel.text = [NSString stringWithFormat:@"%@", conversation.unreadCount];
    } else {
        [self.messageNumberLabel setHidden:YES];
    }
    
    if (conversation.latestMessage.timestamp != nil ) {
        double time = [conversation.latestMessage.timestamp doubleValue];
        self.time.text = @"这是一个时间";
    } else {
        self.time.text = @"无时间";
    }
    
    if (conversation.unreadCount.integerValue) {
        _messageNumberLabel.hidden = NO;
        _messageNumberLabel.text = [NSString stringWithFormat:@"%@", conversation.unreadCount];
    }else{
        _messageNumberLabel.hidden = YES;
    }
    
    self.message.text = @"aaa";
    
}

- (NSString *)conversationIdWithConversation:(JMSGConversation *)conversation {
    NSString *conversationId = nil;
    if (conversation.conversationType == kJMSGConversationTypeSingle) {
        JMSGUser *user = conversation.target;
        conversationId = [NSString stringWithFormat:@"%@_%ld_%@",user.username, kJMSGConversationTypeSingle, conversation.targetAppKey];
    } else {
        JMSGGroup *group = conversation.target;
        conversationId = [NSString stringWithFormat:@"%@_%ld",group.gid,kJMSGConversationTypeGroup];
    }
    return conversationId;
}

- (void)layoutSubviews{
    self.headView.layer.cornerRadius = 23;
    [self.headView.layer setMasksToBounds:YES];
    _headView.size = CGSizeMake(46, 46);
    _headView.left = 10;
    _headView.centerY = self.height / 2.0;
    
    [_nickName sizeToFit];
    _nickName.top = _headView.top;
    _nickName.left = _headView.right + 10;
    
    [_message sizeToFit];
    _message.top = _nickName.bottom + 5;
    _message.left = _nickName.left;
    
    [_time sizeToFit];
    _time.right = self.width - 10;
    _time.centerY = self.height / 2.0;
    
    _messageNumberLabel.size = CGSizeMake(15, 15);
    _messageNumberLabel.top = _headView.top - 2;
    _messageNumberLabel.right = _headView.right + 2;
    [self.messageNumberLabel.layer setMasksToBounds:YES];
    self.messageNumberLabel.layer.cornerRadius = _messageNumberLabel.width / 2.0;
    self.messageNumberLabel.layer.borderWidth = 1;
    self.messageNumberLabel.layer.borderColor = [UIColor whiteColor].CGColor;
    self.messageNumberLabel.textAlignment = NSTextAlignmentCenter;
    [self.messageNumberLabel setBackgroundColor:UIColorHex(0xfa3e32)];
    self.messageNumberLabel.textColor = [UIColor whiteColor];
    _messageNumberLabel.font = [UIFont systemFontOfSize:10];
}

@end
